<?php
namespace addons\caipiao\controller;

use addons\caipiao\model\Cplist;
use addons\caipiao\service\CplistService;
use addons\caipiao\service\Qiyezf;
use app\common\controller\Addon;
use think\App;
use think\Db;
use think\facade\Log;
use think\facade\Request;

class Index extends Addon{
    public $adminLogin=true;//注意：因为这是后台业务菜单所以需要管理员登录操作的
    private $cplistModel;
    private $cplistS;
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->cplistModel = new Cplist();
        $this->cplistS = new CplistService();
    }

    public function index(){
        $url = urlAddons('caipiao/Index/getList');
        $this->assign('url',$url);
        return $this->fetch();
    }
    //获取兑奖列表数据
    public function getList(){
        $page=input('page',1,'int');
        $limit = input('limit',10,'int');
        $keyword = input('keyword','','string');
        $status = input('status',-1,'int');
        $data = $this->cplistS->lists($keyword,$page,$limit,$status);
        $count = $this->cplistS->getCount($keyword,$status);
        return json([
            'code'=>0,
            'msg'=>'获取数据成功',
            'count'=>$count,
            'data'=>$data,
            'limit'=>$limit
        ]);
    }
    //添加兑奖数据
    public function add(){
        if(Request::method()=='POST'){
            $data=[
                'relname'=>Request::put('relname'),
                'openid'=>Request::put('openid'),
                'img_url'=>Request::put('imgurl'),
                'balance'=>Request::put('balance'),
                'mobile'=>Request::put('tel')
            ];
            //判断
            $r = $this->cplistS->add($data);
            if($r){
                return json([
                    'code'=>0,
                    'msg'=>'ok'
                ]);
            }else{
                return json([
                    'code'=>-1,
                    'msg'=>'error'
                ]);
            }
        }else{
            return $this->fetch();
        }
    }
    //编辑
    public function edit(){
        if(Request::instance()->isPost()){
            $data=[
                'id'=>Request::put('id'),
                'relname'=>Request::put('relname'),
                'openid'=>Request::put('openid'),
                'img_url'=>Request::put('imgurl'),
                'balance'=>Request::put('balance'),
                'mobile'=>Request::put('mobile'),
                'status'=>Request::put('status')
            ];
            $r = $this->cplistS->edit($data);
            if($r){
                return json([
                    'code'=>0,
                    'msg'=>'ok'
                ]);
            }else{
                return json([
                    'code'=>-1,
                    'msg'=>'error'
                ]);
            }
        }else{
            $id = input('id',0,'int');
            $page = input('page',1,'int');
            $limit = input('limit',10,'inst');
            $r = Cplist::with('mpfriends')
                ->where(['id'=>$id])
                ->find();
            $r['page']=$page;
            $r['limit']=$limit;
            $this->assign('data',$r);
            return $this->fetch();
        }
    }
    //删除
    public function delete(){
        if(Request::instance()->isDelete()){
            $id = input('id',0,'int');
            $r=$this->cplistModel->where(['id'=>$id])->delete($id);
            if($r){
                return json(['code'=>0,'msg'=>'成功删除']);
            }else{
                return json(['code'=>-1,'msg'=>'删除失败']);
            }
        }
        return json(['code'=>-2,'msg'=>'未知请求']);
    }
    //提现
    public function cash(){
        if(Request::instance()->isPost()){
            $id = input('id',0,'int');
            $qiyezf = new Qiyezf();
            $r = $this->cplistModel->where(['id'=>$id])->field('cash_type,balance,openid,status')->find();
            if($r['cash_type'] == 0 && $r['status'] == '中奖'){
                $partner_trade_no = date('YmdHis').rand(100, 999).$id;
                $result = $qiyezf->sendMoney($r['balance'],$r['openid'],$partner_trade_no,'彩票提现');
//                Log::notice($result);
                if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'
                && $this->cplistS->cashSuccess($id,$partner_trade_no)){
                    //提现成功执行业务逻辑
                    return json([
                        'code'=>0,
                        'msg'=>'提现成功'
                    ]);
                }else{
                    return json(['code'=>1,'msg'=>$result['err_code_des']]);
                }
            }
            return json(['code'=>1,'msg'=>'不符合提现条件']);
        }
        return json(['code'=>-2,'msg'=>'未知请求']);
    }
}
